package com.hntky.controller;

import cn.hutool.core.util.StrUtil;
import com.hntky.common.Result;
import com.hntky.controller.dto.UserDTO;
import com.hntky.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author 周宇轩
 * @date 2023/5/1 15:57
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if(StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            // 用户名或者密码为空时
            return Result.error("400", "参数有误");
        }
        return Result.success(userService.login(userDTO));
    }

    @PostMapping("/add")
    public Result register(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if(StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            // 用户名或者密码为空时
            return Result.error("400", "参数有误");
        }
        return Result.success(userService.insertUser(userDTO));
    }

    @PostMapping("/upload")
    public Result upload(@RequestBody UserDTO userDTO) {
        return Result.success(userService.updateUser(userDTO));
    }
}
